package com.zbkj.admin.controller.platform;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.zbkj.common.model.saleroom.SaleRoom;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.result.CommonResult;
import com.zbkj.service.service.saleroom.ISaleRoomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 门店信息Controller
 *
 * @author zkf
 * @date 2025-02-21
 */
@Api("门店信息主表")
@RestController
@RequestMapping("api/admin/platform/room")
public class SaleRoomController {

    @Autowired
    private ISaleRoomService saleRoomService;

    /**
     * 查询门店信息主列表
     */
    @ApiOperation("查询门店信息列表")
    @PreAuthorize("hasAuthority('platform:room:list')")
    @GetMapping("/list")
    public CommonResult<CommonPage<SaleRoom>> list(SaleRoom saleRoom, PageParamRequest pageParamRequest) {
        return CommonResult.success(CommonPage.restPage(saleRoomService.selectSaleRoomList(saleRoom, pageParamRequest)));
    }

//    /**
//     * 导出门店信息主列表
//     */
//    @ApiOperation("导出门店信息主列表")
//    @PreAuthorize("hasAuthority('sale:room:export')")
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, SaleRoom saleRoom) {
//        List<SaleRoom> list = saleRoomService.selectSaleRoomList(saleRoom);
//        ExcelUtil<SaleRoom> util = new ExcelUtil<SaleRoom>(SaleRoom.class);
//        util.exportExcel(response, list, "门店信息主数据");
//    }

    /**
     * 获取门店信息主详细信息
     */
    @ApiOperation("获取门店信息详细信息")
    @PreAuthorize("hasAuthority('platform:room:query')")
    @GetMapping(value = "/{id}")
    public CommonResult<SaleRoom> getInfo(@PathVariable("id") Long id) {
        Assert.isTrue(!ObjectUtils.isEmpty(id), "门店id为空！");
        SaleRoom saleRoom = saleRoomService.getById(id);
        String deliveryRange = saleRoom.getDeliveryRange();
        if (StrUtil.isNotBlank(deliveryRange)) {
            List<Double[]> deliveryRangeList = JSONArray.parseArray(deliveryRange, Double[].class);
            saleRoom.setDeliveryRangeList(deliveryRangeList);
        }
        return CommonResult.success(saleRoom);
    }

    /**
     * 新增门店信息主
     */
    @ApiOperation("新增门店信息")
    @PreAuthorize("hasAuthority('platform:room:add')")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody SaleRoom saleRoom) {
        saleRoom.setCreateTime(DateUtil.now())
                .setDeliveryRange(JSONArray.toJSONString(saleRoom.getDeliveryRangeList()));
        if (saleRoomService.save(saleRoom)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 修改门店信息主
     */
    @ApiOperation("修改门店信息")
    @PreAuthorize("hasAuthority('platform:room:edit')")
    @PutMapping("/edit")
    public CommonResult<String> edit(@RequestBody @Valid SaleRoom saleRoom) {
        saleRoom.setUpdateTime(DateUtil.now())
                .setDeliveryRange(JSONUtil.toJsonStr(saleRoom.getDeliveryRangeList()));
        if (saleRoomService.updateById(saleRoom)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    /**
     * 删除门店信息主
     */
    @ApiOperation("删除门店信息")
    @PreAuthorize("hasAuthority('platform:room:remove')")
    @DeleteMapping("/{ids}")
    public CommonResult<String> remove(@PathVariable Long[] ids) {
        Assert.isTrue(!ObjectUtils.isEmpty(ids), "门店id为空！");
        if (saleRoomService.deleteSaleRoomByIds(ids) >= 1) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    @ApiOperation("获取所有门店配送范围列表(经纬度数组对象列表)")
    @PreAuthorize("hasAuthority('platform:room:delivery_range:list')")
    @GetMapping(value = "/delivery_range/list")
    public CommonResult<List<List<Double[]>>> getDeliveryRangeList() {
        return CommonResult.success(saleRoomService.getDeliveryRangeList());
    }


}